package com.kamistoat.meimeimall.meimeimallorder.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class OrderThreadConfig {
    /**
     * 创建订单时的线程池，用于异步查询订单信息。
     */
    @Bean("orderCreateQueryThreadPoolExecutor")
    public ThreadPoolExecutor orderCreateQueryThreadPoolExecutor(){
        return new ThreadPoolExecutor(
                8,
                16,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    /**
     * 对订单执行缓存预扣除
     */
    @Bean("orderCacheDeductThreadPoolExecutor")
    public ThreadPoolExecutor orderCacheDeductThreadPoolExecutor(){
        return new ThreadPoolExecutor(
                8,
                16,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    /**
     * 业务层宕机后的补偿线程池
     */
    @Bean("workCrashThreadPoolExecutor")
    public ThreadPoolExecutor workCrashThreadPoolExecutor(){
        return new ThreadPoolExecutor(
                8,
                16,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }


    /**
     * 任务引擎线程池
     */
    @Bean
    public ThreadPoolExecutor orderTaskEngineThreadPoolExecutor() {
        return new ThreadPoolExecutor(
                9,
                9,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
